import re
import time

import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent


class song(object):
    def __init__(self):
        self.China_EDM_id = '10520166'
        self.World_EDM_id = '1978921795'

    def spider(self, list_id):
        time.sleep(1)
        url = 'https://music.163.com/playlist'
        headers = {
            "cookie":"_ntes_nnid=5c99ad045581e064fad769bab5315216,1609045511812; _ntes_nuid=5c99ad045581e064fad769bab5315216; NMTID=00OwWXDFshRyeoKY0FRufLJKpoNU-AAAAF2opYJaA; WM_TID=vZ85Qxp2ap9EQBUQVAJqOdwZ1jgU7CGI; ntes_kaola_ad=1; _iuqxldmzr_=32; __csrf=40362ea41bd4a5025b0ad43dbe2daa81; MUSIC_U=7b556cee10dece13cf2d554a70f574c56fa8e7110d97bedf8da5adb9834c824433a649814e309366; hb_MA-B407-E266474A0BB8_source=www.163yun.com; WM_NI=0GRxyR%2B%2B0fOGudhm5Flodb7qDB2bx6dWaDCPQdwNaJeihbr3dxnGXXXDqu9HHkCHr6GsArUtTl8O1%2BQm8SW9TWf1B1e%2FHd%2F%2FrKHiUABZTIsvNexLq%2FHX4DcujWz%2BQS%2BMdmc%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6eeb5bc539c928e8cbc6090a88ab2d14e869a9eaef16d98b7a3a3d36eb5ebbcade12af0fea7c3b92af4999e99fc5ae9f50087ce4db7bca88eb84eb0b1ac83b67b889efe83fb3ea889afa5c25c93bcb9afe442a7b7a883f139a8a8a2a8db549c9cbf96bc448cb58da9f13e85ad8f93d88098b4f8b0b42193b3f8b4d6339889aad2c13ba7f18885ae25b295888ad84b82869caaf27e8ebbb8adf4258c88fca5e27faa89b6ccd459e993ac8ee237e2a3; WEVNSM=1.0.0; WNMCID=vzmwiy.1616737327232.01.0; JSESSIONID-WYYY=m3KNiFwjlAVeBs%5CFjeA%2BD1Pk0i7rI%5CPeWXaM9nFIl4YTTgBsUcPQHwcWch9OIij6UYxzomdjDUTm0G5Ga68yslHsW8wGnV5kr2vnDiJ57WD3WuzZr4ECSPbGDqyn3%2FJn3oQ4%5CsGYji9uJ9YJG8CZMt0SafK%5C9NQFyRUZu%5CXe4dNbDO4N%3A1616746084368",
            "dnt": "1",
            "referer": "https://music.163.com /",
            'User-Agent': UserAgent().random}

        params = {'id': list_id}
        response = requests.get(url=url,params=params,  headers=headers).content.decode()
        return response

    def parse(self, response):
        song_id = []
        song_name = []

        soup = BeautifulSoup(response, 'lxml')
        '''
        <ul class="f-hide">
        <li><a href="/song?id=1828542977>On My Own</a></li>
        <li><a href="/song?id=1824974288">Heart of Mine</a></li>
        <li><a href="/song?id=1828102345">Complicated</a></li>
        <li><a href="/song?id=1825668326">Dance All Night</a></li>
        <li><a href="/song?id=1825558794">Reborn</a></li>
        <li><a href="/song?id=1824961569">Space</a></li>
        <li><a href="/song?id=1824513851">Polaris</a></li>
        <li><a href="/song?id=1823468454">HELLO 2021</a></li>
        <li><a href="/song?id=1822275588">NoBody But U</a></li>
        <li><a href="/song?id=1823147106">拱极星座</a></li>
        <li><a href="/song?id=1822450573">Give Me Love</a></li>
        <li><a href="/song?id=1822267858">传奇</a></li>
        <li><a href="/song?id=1821670007">ROSE（你的玫瑰）</a></li>
        <li><a href="/song?id=1821001542">深渊</a></li>
        <li><a href="/song?id=1820328617">烟花昭</a></li>
        <li><a href="/song?id=1820314460">出现</a></li>
        <li><a href="/song?id=1820297964">Anabiosis Rite</a></li>
        <li><a href="/song?id=1822495129">Trip Over (NeonSekai Remix)</a></li>
        <li><a href="/song?id=1822496937">Sing &amp; Dancing (AiryDisc Remix)</a></li>
        <li><a href="/song?id=1493730146">Phoenix</a></li>
        </ul>
        ''' #soup.ul结果（国电示范）
        script = soup.ul.find_all('li')
        rs = re.findall(r'<li><a href="(.*?)</a></li>', str(script))
        for i in rs:
            song_id.append((re.findall(r'id=(.+)">.*?', i))[0])
            song_name.append((re.findall(r'id=.*?">(.+)', i))[0])

        print(rs)
        print(song_id)
        print(song_name)


    def China_EDM(self):
        response_China = self.spider(list_id=self.China_EDM_id)

        China_EDM.parse(response=response_China)


    def World_EDM(self):
        response_World = self.spider(list_id=self.World_EDM_id)

        China_EDM.parse(response=response_World)


if __name__ == "__main__":
    China_EDM = song()
    China_EDM.China_EDM()


    World_EDM = song()
    World_EDM.World_EDM()


    print('over!')



